cmake_minimum_required(VERSION 3.28)
project(MusicPlayer)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# 设置Qt安装路径
set(CMAKE_PREFIX_PATH "D:/software/Qt6.7/6.7.2/mingw_64")

# 查找并包含所需的Qt模块
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Quick Qml QuickControls2 Core5Compat Network)

# 添加可执行文件及其源文件
add_executable(MusicPlayer
        main.cpp
        qresource.qrc
        resources/icon/icon.rc
        src/utils/httputils.cpp
        src/utils/httputils.h
)

# 链接Qt模块到目标
target_link_libraries(MusicPlayer
        PRIVATE Qt6::Core
        PRIVATE Qt6::Gui
        PRIVATE Qt6::Widgets
        PRIVATE Qt6::Quick
        PRIVATE Qt6::Qml
        PRIVATE Qt6::QuickControls2
        PRIVATE Qt6::Core5Compat
        PRIVATE Qt6::Network
)

# 复制必要的Qt DLL文件
if (WIN32)
    # 设置调试后缀
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()

    # 设置Qt安装路径
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")

    # 如果bin目录不存在，则向上查找
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()

    # 复制 GraphicalEffects 模块
    add_custom_command(TARGET MusicPlayer POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_directory
            "D:/software/Qt6.7/6.7.2/mingw_64/qml/Qt5Compat/GraphicalEffects"
            "$<TARGET_FILE_DIR:MusicPlayer>/qml/Qt5Compat/GraphicalEffects"
    )

    # 复制每个Qt模块的DLL文件
    foreach (QT_LIB Core Gui Widgets Quick Qml QuickControls2)
        add_custom_command(TARGET MusicPlayer POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:MusicPlayer>"
        )
    endforeach ()

    # 复制Qt5Compat.GraphicalEffects/private目录中的文件到项目目录
    file(GLOB_RECURSE PRIVATE_FILES
            "${QT_INSTALL_PATH}/qml/Qt5Compat/GraphicalEffects/private/*"
    )
    file(COPY ${PRIVATE_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/qml/Qt5Compat/GraphicalEffects/private")

endif ()
